package com.example.version.controll.controller;

import com.example.version.controll.model.RepositoryConfig;
import com.example.version.controll.util.FileUtil;
import com.example.version.controll.util.GitUtil;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) throws GitAPIException, IOException {
        RepositoryConfig repositoryConfig = FileUtil.configDTO.getRepositoryList().get(0);
        GitUtil gitUtil = new GitUtil(repositoryConfig);
        System.out.println("//////");
//        gitUtil.getAllTags();
        System.out.println("//////");
//        gitUtil.createTag("V1.0.1", "dev_jsp");
        gitUtil.delTag("V6.0","V7.0");
        List<Ref> allTags = gitUtil.getAllTags();
        System.out.println(allTags.stream().map(Ref::getName).collect(Collectors.joining(",")));
//        gitUtil.pushTag();
        System.out.println("//////+++");
//        gitUtil.getAllTags();
        System.out.println("//////+++");
//        System.out.println(GitUtil.getRemoteBranchs("https://gitee.com/ycx007/wc.git").stream().collect(Collectors.joining(",")));
//        System.out.println(GitUtil.getLocalBranchList("C:\\Users\\ljs\\IdeaProjects\\version-controll\\target\\gitData\\SimpleDevelopForSmartbi").stream().collect(Collectors.joining(",")));
    }

    public static List<String> getRemoteBranchs(String url) {
        try {
            Collection<Ref> refList;
            UsernamePasswordCredentialsProvider pro = new UsernamePasswordCredentialsProvider(FileUtil.configDTO.getUserName(), FileUtil.configDTO.getPassWord());
            refList = Git.lsRemoteRepository().setRemote(url).setCredentialsProvider(pro).call();
            return refList.stream().map(Ref::getName).filter(x -> x.startsWith("refs/heads/")).map(x -> x.replace("refs/heads/", "")).collect(Collectors.toList());
        } catch (Exception e) {
            System.out.println("error");
        }
        return Collections.emptyList();
    }
}
